package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Api(tags = "数据字典管理")
@RestController
@RequestMapping(value = "/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictService dictService;


    @ApiOperation(value = "根据上级id获取子节点数据列表")
    @ApiImplicitParam(name = "parentId",value = "上级节点id",paramType = "path",required = true,dataType = "long")
    @GetMapping("findChildData/{parentId}")
    public Result<List<Dict>> findChildData(@PathVariable Long parentId){
        List<Dict> dictList = dictService.findChildData(parentId);
        return Result.ok(dictList);
    }

    //导出数据字典接口
    @ApiOperation("导出数据字典接口")
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response) throws IOException {
        dictService.exportData(response);
    }

    //导入数据字典
    @ApiOperation("导入字典数据")
    @PostMapping("importData")
    @CacheEvict(value = "dict",allEntries = true)
    public Result importDict(MultipartFile file) {
        dictService.importDictData(file);
        return Result.ok();
    }
    /**跟dictcode 和 value查询**/
    @ApiOperation("跟dictcode 和 value查询")
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,@PathVariable String value){
        /**如果dictCode为空，直接根据value查询**/
        String dictName = dictService.getDictName(dictCode,value);
        /**dictCode为空，直接根据value查询**/
        return dictName;
    }

    /**根据value**/
    @ApiOperation("根据value查询")
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value){
        String dictName = dictService.getDictName("",value);
        return  dictName;
    }

    /**根据数据id查询子数据列表**/
    @ApiOperation("根据数据id查询子数据列表")
    @GetMapping("findByChildData/{dictCode}")
    public Result findByDictdCode(@PathVariable String dictCode){
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }
}
